package com.yl.chainStore.services.impl.pay

import com.yl.chainStore.common.enums.Status
import com.yl.chainStore.entity.pay.PayCustomerOrderItemDiscountRule
import com.yl.chainStore.mapper.pay.PayCustomerOrderItemDiscountRuleMapper
import com.yl.chainStore.services.pay.IPayCustomerOrderItemDiscountRuleService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.util.*

@Service
class PayCustomerOrderItemDiscountRuleServiceImpl(
	private val payCustomerOrderItemDiscountRuleMapper: PayCustomerOrderItemDiscountRuleMapper
):IPayCustomerOrderItemDiscountRuleService {
	companion object {
		private val log = LoggerFactory.getLogger(IPayCustomerOrderItemDiscountRuleService::class.java)
	}
	override fun del(id: Long): ResponseBody {
		log.info("订单项目优惠 ==> 删除 id{}",id)
		val rule =
			payCustomerOrderItemDiscountRuleMapper.selectById(id) ?: return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)

		if(rule.enabled == 0){
			return returnSuccess()
		}
		rule.enabled = 0
		rule.updateTime = Date()
		rule.updateUserId = getContext().userId
		payCustomerOrderItemDiscountRuleMapper.updateById(rule)
		return returnSuccess()
	}

	override fun resetTime(id: Long,startTime: Date, endTime: Date): ResponseBody {
		log.info("订单项目优惠 ==> 重设时间 id：{}，startTime：{}，endTime:{}",id,startTime,endTime)
		val rule =
			payCustomerOrderItemDiscountRuleMapper.selectById(id) ?: return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		if(rule.enabled == 0){
			return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		}
		rule.startTime = startTime
		rule.endTime = endTime
		rule.updateTime = Date()
		rule.updateUserId = getContext().userId
		payCustomerOrderItemDiscountRuleMapper.updateById(rule)
		return returnSuccess()
	}

	override fun openRule(id: Long): ResponseBody {
		log.info("订单项目优惠 ==> 开启折扣 id：{}，",id)
		val rule =
			payCustomerOrderItemDiscountRuleMapper.selectById(id) ?: return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		if(rule.enabled == 0){
			return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		}
		rule.status = Status.OPEN
		rule.updateTime = Date()
		rule.updateUserId = getContext().userId
		payCustomerOrderItemDiscountRuleMapper.updateById(rule)
		return returnSuccess()
	}

	override fun closeRule(id: Long): ResponseBody {
		log.info("订单项目优惠 ==> 关闭折扣 id：{}",id)
		val rule =
			payCustomerOrderItemDiscountRuleMapper.selectById(id) ?: return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		if(rule.enabled == 0){
			return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		}
		rule.status = Status.CLOSE
		rule.updateTime = Date()
		rule.updateUserId = getContext().userId
		payCustomerOrderItemDiscountRuleMapper.updateById(rule)
		return returnSuccess()
	}

	override fun updateInfo(rule: PayCustomerOrderItemDiscountRule): ResponseBody {
		log.info("订单项目优惠 ==> 修改基础信息 rule：{}",rule)
		val updateRule =
			payCustomerOrderItemDiscountRuleMapper.selectById(rule.id) ?: return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		if(rule.enabled == 0){
			return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		}
		updateRule.title = rule.title
		rule.updateTime = Date()
		rule.updateUserId = getContext().userId
		payCustomerOrderItemDiscountRuleMapper.updateById(updateRule)
		return returnSuccess()
	}

	override fun updateRule(id:Long,rule:Any): ResponseBody {
		log.info("订单项目优惠 ==> 修改规则 id：{}，rule:{}",id,rule)
		val updateRule=
			payCustomerOrderItemDiscountRuleMapper.selectById(id) ?: return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		if(updateRule.enabled == 0){
			return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		}
		updateRule.other = rule
		val validationSelf = updateRule.getOtherContentObj().validationSelf()
		if(validationSelf.bodyIsError()){
			return validationSelf
		}
		updateRule.updateTime = Date()
		updateRule.updateUserId = getContext().userId
		payCustomerOrderItemDiscountRuleMapper.updateById(updateRule)
		return returnSuccess()
	}
}
